<?php

abstract class a_base_module extends a_base_model implements i_module_base {
    private $_name;
    private $_description;
    private $_installed_version;
    private $_module_settings;
 
    public function __construct($basic_info=NULL) {        
        parent::__construct(FALSE);
        $this->ci()->load->helper("toolbox");
        if($basic_info) {
            $this->_description = $basic_info->description;
            $this->_installed_version = $basic_info->version;
            $this->_name = $basic_info->name;
            $this->load_settings();
            event_dispatcher::i()->attach_module_event_subscribers($this->get_name());
            
        }else{
            show_error("Basic information for a module is missing.Please check module table.");
        }
    }

    public function get_description() {
        return $this->_description;
    }
    public function get_name() {
        return $this->_name;
    }
    public function get_current_version() {
        throw new Exception("Not implemented yet");
    }
    public function get_installed_version() {
        return $this->_installed_version;
    }
    public function get_dependency() {
        throw new Exception("Not implemented yet");
    }
    public function get_real_name() {
        return "module_u_".$this->_name."";
    }
    public function get_settings(){
        return $this->_module_settings;
    }
    public function get_setting($name){
        if(property_exists($this->_module_settings, $name)){
            $setting = $this->_module_settings->$name;
            if(strlen($setting->value)){
                return $setting->value;
            }else{
                return $setting->default;
            }
        }else{
            return NULL;
        }
    }
    
    protected function load_settings(){
        $query =
        $this->db()
                ->where("module_name",$this->get_name())
                        ->get("modules_settings");
        if($query->num_rows()>0){
            $this->_module_settings = new settings_m_core($query->result());
        }else{
            $this->_module_settings = new stdClass();
        }
    }
    protected function smarty(){
        return get_instance()->smarty_wrapper;
    }
    public function is_admin() {
        $segments = explode("/", $this->ci()->uri->uri_string());        
        return  @($segments[2]==="admin");
    }
}
?>